package com.qdu.controller;

import com.qdu.entity.Replyinfo;
import com.qdu.entity.Userinfo;
import com.qdu.service.ReplyService;
import com.qdu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/replys")
public class ReplyController {

    @Autowired
    private ReplyService replyService;

    @Autowired
    private UserService userService;

    @PostMapping("/delete")
    public void deleteByPrimaryKey(String rid){

    }

    @PostMapping("/add")
    public void insert(Replyinfo record){
        record.setRdate(new Date());
        replyService.insert(record);
    }

    @PostMapping("/addselective")
    public void insertSelective(Replyinfo record){

    }

    @GetMapping("/getbyrid")           /*通过此回复对象的id查找*/
    public HashMap<String,Object> selectByPrimaryKey(String rid){
        HashMap<String,Object> result=new HashMap<>();

        Replyinfo replyinfo=replyService.selectByPrimaryKey(rid);
        Userinfo replyuser=userService.selectByPrimaryKey(replyinfo.getPid());
        Userinfo user=userService.selectByPrimaryKey(replyinfo.getUid());

        result.put("beihuifuzhe",user);
        result.put("huifuzhe",replyuser);
        result.put("huifubiao",replyinfo);

        return result;
    }

    @GetMapping("getbycid")            /*通过此回复所在留言主板的id查找*/
    public List selectByCid(String cid){
        List<Replyinfo> replyinfos=replyService.selectByCid(cid);
        List<Map> results=new ArrayList<>();
        for(Replyinfo r:replyinfos){
            HashMap<String,Object> result=new HashMap<>();

            Userinfo replyuser=userService.selectByPrimaryKey(r.getPid());
            Userinfo user=userService.selectByPrimaryKey(r.getUid());

            result.put("beihuifuzhe",user);
            result.put("huifuzhe",replyuser);
            result.put("huifubiao",r);

            results.add(result);
        }

        return results;
    }

    @GetMapping("/getbypid")           /*通过发表此回复的用户id查找*/
    public List selectByPid(String pid){
        List<Replyinfo> replyinfos=replyService.selectByPid(pid);
        List<Map> results=new ArrayList<>();
        for(Replyinfo r:replyinfos){
            HashMap<String,Object> result=new HashMap<>();

            Userinfo replyuser=userService.selectByPrimaryKey(r.getPid());
            Userinfo user=userService.selectByPrimaryKey(r.getUid());

            result.put("beihuifuzhe",user);
            result.put("huifuzhe",replyuser);
            result.put("huifubiao",r);

            results.add(result);
        }

        return results;
    }

    @GetMapping("/getbyuid")           /*通过被回复的用户id查找*/
    public List selectByUid(String uid){
        List<Replyinfo> replyinfos=replyService.selectByUid(uid);
        List<Map> results=new ArrayList<>();
        for(Replyinfo r:replyinfos){
            HashMap<String,Object> result=new HashMap<>();

            Userinfo replyuser=userService.selectByPrimaryKey(r.getPid());
            Userinfo user=userService.selectByPrimaryKey(r.getUid());

            result.put("beihuifuzhe",user);
            result.put("huifuzhe",replyuser);
            result.put("huifubiao",r);

            results.add(result);
        }

        return results;
    }
}
